//
//  MapController.m
//  testTry
//
//  Created by apple on 2017/3/10.
//  Copyright © 2017年 CollectionView. All rights reserved.
//

#import "MapController.h"
#import<AMapNaviKit/AMapNaviKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

@interface MapController ()<AMapNaviDriveViewDelegate,AMapNaviDriveManagerDelegate,MAMapViewDelegate>
@property(nonatomic,strong)AMapNaviDriveView*driveView;
@property(nonatomic,strong)AMapNaviDriveManager*driveManager;
@property(nonatomic,strong)AMapNaviPoint* startPoint;
@property(nonatomic,strong)AMapNaviPoint* endPoint;
@property(nonatomic,strong)MAMapView *mapView;
@property(nonatomic,strong)CLLocation *currentLocation;
@end
@implementation MapController
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveLocate:) name:@"locate" object:nil];
    
    [AMapServices sharedServices].apiKey = @"7394ee7e01f14728a728690d71513bc3";

    self.view.backgroundColor = [UIColor whiteColor];
    [self initDriveView];
    [self initDriveManager];
//    [self initMapView];
    [self initProperties];
//    [self initMapView];
    
    [self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint]
                                                endPoints:@[self.endPoint]
                                                wayPoints:nil
                                          drivingStrategy:17];
    
    [self driveManagerOnCalculateRouteSuccess:self.driveManager];
    
}
-(void)receiveLocate:(NSNotification*)noti{
    CLLocation *currentLocation = noti.userInfo[@"location"];
    self.currentLocation = currentLocation;
}
- (void)initDriveView
{
    if (self.driveView == nil)
    {
        self.driveView = [[AMapNaviDriveView alloc] initWithFrame:self.view.bounds];
        self.driveView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.driveView setDelegate:self];
        
        [self.view addSubview:self.driveView];
    }
}

- (void)initDriveManager
{
    if (self.driveManager == nil)
    {
        self.driveManager = [[AMapNaviDriveManager alloc] init];
        [self.driveManager setDelegate:self];
        
        //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
        [self.driveManager addDataRepresentative:self.driveView];
    }
}



- (void)initProperties

{
//    self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
    self.startPoint = [AMapNaviPoint locationWithLatitude:31.65 longitude:120.75 ];
    self.endPoint   = [AMapNaviPoint locationWithLatitude:31.14 longitude:120.63];
}

- (void)initMapView
{
    if (self.mapView == nil)
    {
        self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0,
                                                                   self.view.bounds.size.width,
                                                                   self.view.bounds.size.height )];
        [self.mapView setDelegate:self];
        [self.view addSubview:self.mapView];
    }
}
- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager
{
    
    //显示路径或开启导航
    [self.driveManager startGPSNavi];

}



@end
